package priv.pront.code.national.acwing.dp.interval;

import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-05-23 19:42
 */
public class Acwing320_能量项链 {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] a = new int[2 * n + 1];
        int[][] dp = new int[2 * n + 1][2 * n + 1];
        for(int i = 1; i <= n;i++){
            a[i] = scanner.nextInt();
            a[i + n] = a[i];
        }
        for(int len = 3; len <= n + 1; len++){
            for(int i = 1; i + len - 1 <= 2 * n; i++){
                int j = i + len - 1;
                for(int k = i + 1; k < j; k++){
                    dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[k][j] + a[i] * a[k] * a[j]);
                }
            }
        }
        int ans = 0;
        // 枚举起点
        for(int i = 1; i <= n; i ++){
            ans = Math.max(ans, dp[i][i + n]);
        }
        System.out.println(ans);
    }
}
